using System;
using System.Collections.Generic;
using Infragistics.UltraChart.Resources.Appearance;
using PluginFrame;

namespace PcrDataAnalysis
{
	public class HRMCurve : IHRMCurve
	{
		private XYSeries mCurve;

		private IPcrCell mPcrCell;

		protected Dictionary<IHRMCurve, double> mDistanceDictionary;

		public XYSeries CurveXYSeries
		{
			get
			{
				return mCurve;
			}
			set
			{
				mCurve = value;
			}
		}

		public IPcrCell PcrCell
		{
			get
			{
				return mPcrCell;
			}
		}

		public int ID
		{
			get
			{
				return mPcrCell.CellID;
			}
		}

		public virtual int GetCurveCount()
		{
			return 1;
		}

		public override string ToString()
		{
			return mPcrCell.CellPosition;
		}

		public HRMCurve()
		{
		}

		public HRMCurve(IPcrCell pcrCell, XYSeries curve)
		{
			mCurve = curve;
			mPcrCell = pcrCell;
			mDistanceDictionary = new Dictionary<IHRMCurve, double>();
		}

		protected virtual double Calculate(IHRMCurve hrmCurve)
		{
			if (hrmCurve.Equals(this))
			{
				return 0.0;
			}
			double num = 0.0;
			if (hrmCurve is MergedHRMCurve)
			{
				MergedHRMCurve mergedHRMCurve = hrmCurve as MergedHRMCurve;
				num = mergedHRMCurve.Calculate(this);
			}
			else
			{
				XYSeries curveXYSeries = hrmCurve.CurveXYSeries;
				int num2 = ((mCurve.Points.Count > curveXYSeries.Points.Count) ? curveXYSeries.Points.Count : mCurve.Points.Count);
				for (int i = 0; i < num2; i++)
				{
					if (!double.IsNaN(mCurve.Points[i].ValueY))
					{
						double.IsNaN(curveXYSeries.Points[i].ValueY);
					}
					num += Math.Pow(mCurve.Points[i].ValueY - curveXYSeries.Points[i].ValueY, 2.0);
				}
			}
			return num;
		}

		public virtual double CalculateDistance(IHRMCurve hrmCurve, out bool isCurveExisted)
		{
			double num = GetDistance(hrmCurve, out isCurveExisted);
			if (!isCurveExisted)
			{
				num = hrmCurve.GetDistance(this, out isCurveExisted);
				if (!isCurveExisted)
				{
					num = Calculate(hrmCurve);
				}
				SetDistance(hrmCurve, num);
			}
			return num;
		}

		public void SetDistance(IHRMCurve hrmCurve, double distance)
		{
			if (mDistanceDictionary.ContainsKey(hrmCurve))
			{
				mDistanceDictionary[hrmCurve] = distance;
			}
			else
			{
				mDistanceDictionary.Add(hrmCurve, distance);
			}
		}

		public double GetDistance(IHRMCurve hrmCurve, out bool isCurveExisted)
		{
			if (mDistanceDictionary.ContainsKey(hrmCurve))
			{
				isCurveExisted = true;
				return mDistanceDictionary[hrmCurve];
			}
			isCurveExisted = false;
			return double.NaN;
		}

		public void RemoveDistance(IHRMCurve hrmCurve)
		{
			if (mDistanceDictionary.ContainsKey(hrmCurve))
			{
				mDistanceDictionary.Remove(hrmCurve);
			}
		}
	}
}
